package top.lossingdawn.antlr4.calculator;

import com.alibaba.fastjson.JSON;
import org.antlr.v4.runtime.tree.ParseTree;
import top.lossingdawn.antlr4.calculator.antlr.CalculatorBaseListener;
import top.lossingdawn.antlr4.calculator.antlr.CalculatorParser;

import java.util.stream.Collectors;

/**
 * @author jiangyj
 * @version 1.0
 * @create 2021-12-10 14:07
 **/
public class CalculatorListener extends CalculatorBaseListener {

    int result = 0;

    @Override
    public void enterPrintExpr(CalculatorParser.PrintExprContext ctx) {
        super.enterPrintExpr(ctx);
    }

    @Override
    public void enterParens(CalculatorParser.ParensContext ctx) {
        super.enterParens(ctx);
    }

    @Override
    public void enterAddSub(CalculatorParser.AddSubContext ctx) {
        super.enterAddSub(ctx);
        System.out.println("pre:" + ctx.getParent().getText());
        System.out.println("next:" + JSON.toJSONString(ctx.children.stream().map(ParseTree::getText).collect(Collectors.toList())));
    }

    @Override
    public void exitAddSub(CalculatorParser.AddSubContext ctx) {
        super.exitAddSub(ctx);
    }
}
